/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.Component;
import org.aswing.Event;
import org.aswing.JList;
import org.aswing.JTable;
import org.aswing.JTree;
import org.aswing.ListCell;
import org.aswing.ListSelectionModel;
import org.aswing.table.TableCell;
import org.aswing.tree.TreeCell;

import com.bourre.core.HashCodeFactory;
import com.bourre.events.EventType;

import fever.app.menu.FvContextMenu;
import fever.app.menu.FvContextMenuEvent;
import fever.app.menu.FvContextMenuItem;
import fever.utils.Stringifier;

import fvaswing.cells.FvContextMenuCellEvent;
import fvaswing.utils.FvContextMenuUtils;

/**
 * Abstract implementation of an AsWing cell component featuring a contextual 
 * menu management.
 * 
 * <p>Take a look on <strong>Fever</strong> side to see Context menu API.
 * 
 * <p>2 Events type are broadcasted when context menu is engaged.
 * <ul>
 *   <li>{@link fvaswing.cells.FvContextMenuCellEvent#onContextCellMenuEVENT}</li>
 *   <li>{@link fvaswing.cells.FvContextMenuCellEvent#onContextCellItemEVENT}</li>
 * </ul>
 * <br />Broadcasted event is {@link fvaswing.cells.FvContextMenuCellEvent}
 * 
 * <p>Extends this class to create your own cell.
 * 
 * <p>Example 
 * {@code
 *   var itemDelete : FvContextMenuItem = new FvContextMenuItem( "delete" );
 *   var itemDisplay : FvContextMenuItem = new FvContextMenuItem( "show" );
 *  
 *   var list : JList = new JList( 
 * 
 *  	["male", "female", "others ?"], 
 *  	
 *  	new FvListCellFactory ( 
 *  		new FvDefaultContextMenuCell(), 
 *  		[ itemDelete, itemDisplay ]
 *  	)
 *  	
 *   );
 *  
 *   list.addEventListener( FvContextMenuCellEvent.onContextCellItemEVENT, _onListClick, this );
 * } 
 * 
 * @see fvaswing.cells.FvDefaultContextMenuCell
 * 
 * @author Romain Ecarnot
 */
class fvaswing.cells.FvAbstractContextMenuCell implements ListCell, TableCell, TreeCell
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var _owner : Object = new Object();
	
	private var _view : Component;
	private var _model : ListSelectionModel;
	private var _index : Number;	private var _isTree : Boolean;
	private var _context : FvContextMenu;
	private var _selected : Boolean;
	private var _value;
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Simpler this method to speed up performance
	 */
	public function invalidate() : Void
	{
    	_view['valid'] = false;
    }
    
    /**
	 * Simpler this method to speed up performance
	 */
    public function revalidate() : Void
    {
    	_view['valid'] = false;
    }
    
	/**
	 * Sets the value of this cell.
	 * 
	 * <p>Overrides method to keep strong typing with your 
	 * own value type.
	 * 
	 * @param value which should represent on the component of this cell. 
	 */
	public function setCellValue( value ) : Void
	{
		_value = value;
	}
	
	/**
	 * Returns the value of the cell.
	 */
	public function getCellValue()
	{
		return _value;
	}
	
	/**
	 * Returns concrete cell component.
	 * 
	 * <p>Overrides method to keep strong typing with your 
	 * own component.
	 */
	public function getCellComponent() : Component
	{
		return _view;
	}
	
	/**
	 * Sets the list cell status, include the owner-JList isSelected, index position.
	 * 
	 * @param the cell's owner, a JList
	 * @param isSelected true to set the cell selected, false to set not selected.
	 * @param index the row position
	 */
	public function setListCellStatus( component : JList, isSelected : Boolean, index : Number ) : Void
	{
		_setCellStatus( component, isSelected, index );
	}
	
	/**
	 * Sets the table cell status, include the owner-JTable isSelected, row position, column position.
	 * 
	 * @param the cell's owner, a JTable
	 * @param isSelected true to set the cell selected, false to set not selected.
	 * @param row the row position
	 * @param column the column position
	 */
	public function setTableCellStatus( component : JTable, isSelected : Boolean, row : Number, column : Number) : Void
	{
		_setCellStatus( component, isSelected, row, column );
	}
	
	/**
	 * Sets the table cell status, include the owner-JTree isSelected, row position, column position.
	 * 
	 * @param the cell's owner, a JTable
	 * @param seleted true indicated the cell selected, false not selected.
	 * @param expanded true the node is currently expanded, false not.
	 * @param leaf true the node represets a leaf, false not.
	 * @param row the row position
	 */
	public function setTreeCellStatus( tree : JTree , selected : Boolean, expanded : Boolean, leaf : Boolean, row : Number ) : Void
	{
		_isTree = true;
		_setCellStatus( tree, selected, row );
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	private function FvAbstractContextMenuCell( cellComponent : Component, itemList : Array )
	{
		_view = cellComponent;
		_context = _createContextMenu( itemList );
			
		_view.addEventListener( Component.ON_CREATED, _applyContextMenu, this );
	}	
	
	private function _createContextMenu( itemList : Array ) : FvContextMenu
	{
		var m : FvContextMenu = new FvContextMenu ();
		var l : Number = itemList.length;
		for( var i : Number = 0; i < l; i++ ) 
		{
			var item : FvContextMenuItem = itemList[ i ];
			m.push( item, item.id );
		}
		return m;
	}
	
	private function _setCellStatus( component, isSelected : Boolean, index : Number ) : Void
	{
		_selected = isSelected;
		
		if( isSelected )
		{
			_view.setBackground( component.getSelectionBackground() );
			_view.setForeground( component.getSelectionForeground() );
		}
		else
		{
			_view.setBackground( component.getBackground() );
			_view.setForeground( component.getForeground() );
		}
		
		_view.setFont( component.getFont() );
				
		_owner[ HashCodeFactory.getKey( this ) ] = component;
		_model = component.getSelectionModel();
		_index = index;
	}
	
	private function _applyContextMenu( ) : Void
	{
		_context.addEventListener( FvContextMenuEvent.onContextMenuEVENT, this, _onContextOpen );
		_context.addEventListener( FvContextMenuEvent.onContextItemEVENT, this, _onContextClick );
		
		FvContextMenuUtils.applyContextMenu( getCellComponent(), _context.menu );
	}
	
	private function _onContextOpen( event : FvContextMenuEvent ) : Void
	{
		if( _isTree  )
		{
			var tree : JTree = JTree( _owner[ HashCodeFactory.getKey( this ) ] );
			
			if( tree.getSelectionModel().getSelectionMode() != JTree.SINGLE_TREE_SELECTION )
			{
				if( !tree.getSelectionModel().isRowSelected( _index ) )
				{
					tree.setSelectionRow( _index );
				}
			}
			else tree.setSelectionRow( _index );
		}
		else
		{
			if( !_model.isSelectedIndex( _index ) )
				_model.addSelectionInterval( _index, _index );
		}
		
		_dispatchOwnerEvent(  
			_owner[ HashCodeFactory.getKey( this ) ], 
			FvContextMenuCellEvent.onContextCellMenuEVENT, 
			null 
		);
	}
	
	private function _onContextClick( event : FvContextMenuEvent ) : Void
	{
		_dispatchOwnerEvent(  
			_owner[ HashCodeFactory.getKey( this ) ], 
			FvContextMenuCellEvent.onContextCellItemEVENT, 
			event.getItem() 
		);
	}
	
	private function _dispatchOwnerEvent( owner, type : EventType, item : FvContextMenuItem ) : Void
	{
		var e : FvContextMenuCellEvent = new FvContextMenuCellEvent( type, _context, item );
		Component( owner ).dispatchEvent( type, new Event( owner, type, [e] ) );
	}
}